package com.ss.android.article.base.feature.a;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.bytedance.common.utility.collection.MaxSizeLinkedHashMap;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.detail.model.ArticleDetail;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryThread;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.update.b.UpdateItemMgr;
import com.ss.android.c.a.b;
import com.ss.android.common.AbsApiThread;
import com.ss.android.model.AbsArticle;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CmdHandler implements WeakHandler.Callback, b {
    private static CmdHandler instance;
    public final WeakList<a$a> a;
    private final Context c;
    private final LinkedHashMap<String, Long> d;
    private final LinkedHashMap<String, Long> e;
    private final LinkedHashMap<Long, Long> f;
    private WeakHandler g;

    private CmdHandler(Context arg4) {
        this.g = new WeakHandler(Looper.getMainLooper(), ((WeakHandler.Callback) this));
        this.c = arg4.getApplicationContext();
        this.a = new WeakList();
        this.d = new MaxSizeLinkedHashMap(100, 100);
        this.e = new MaxSizeLinkedHashMap(100, 100);
        this.f = new MaxSizeLinkedHashMap(100, 100);
    }

    public static CmdHandler inst(Context context) {
        if (CmdHandler.instance == null) {
            synchronized (CmdHandler.class) {
                if (CmdHandler.instance == null && context != null) {
                    CmdHandler.instance = new CmdHandler(context);
                }
            }
        }

        return CmdHandler.instance;
    }

    public static long a(String arg5) {
        if (android.support.design.a.isTextEmpty(arg5)) {
            return -1;
        } else {
            CmdHandler cmdHandler = CmdHandler.instance;
            if (cmdHandler != null) {
                LinkedHashMap hashMap = cmdHandler.e;
                synchronized (hashMap) {
                    Long v0_3 = cmdHandler.e.get(arg5);
                    if (v0_3 != null) {
                        return v0_3.longValue();
                    } else {
                        return -1;
                    }
                }
            }else{
               return -1;
            }
        }
    }

    private void a(long arg4) {
        Iterator v1 = this.a.iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            if (v0 == null) {
                continue;
            }
            ((CmdHandler.a$a) v0).onAdDeleted(arg4);
        }
    }

    private void a(long arg18, JSONObject arg20, boolean arg21) {
        int v3_4;
        List v2_2;
        Object v2_1;
        long v6;
        LinkedHashMap v9 = arg21 ? this.d : this.e;
        if (arg18 < 0) {
            arg18 = 0;
        }

        if (arg20 != null) {
            HashSet v10 = new HashSet();
            HashMap v11 = new HashMap();
            JSONArray v12 = arg20.optJSONArray("is");
            if (v12 != null) {
                int v2;
                for (v2 = 0; v2 < v12.length(); ++v2) {
                    JSONArray v3 = v12.optJSONArray(v2);
                    if (v3 != null && v3.length() == 2) {
                        long v4 = v3.optLong(0);
                        if (v4 > 0) {
                            v6 = v3.optLong(1);
                            if (v6 <= 0) {
                                ((Set) v10).add(Long.valueOf(v4));
                            } else {
                                Article v3_1 = new Article(v4, v6, 0);
                                ((Map) v11).put(v3_1.getItemKey(), v3_1);
                            }
                        }
                    }
                }
            }

            DBHelper v3_2 = DBHelper.getInstance(this.c);
            Iterator v4_1 = ((Set) v10).iterator();
            do {
                label_51:
                if (!v4_1.hasNext()) {
                    break;
                }

                v2_1 = v4_1.next();
                if (((Long) v2_1).longValue() <= 0) {
                    continue;
                }
                v2_2 = v3_2.a(((Long) v2_1).longValue());
                if (v2_2 == null) {
                    continue;
                }
                Iterator v5 = v2_2.iterator();
                while (true) {
                    if (!v5.hasNext()) {
                        break;
                    }
                    v2_1 = v5.next();
                    if (v2_1 == null) {
                        continue;
                    }
                    ((Map) v11).put(((Article) v2_1).getItemKey(), v2_1);
                }
            } while (true);

            Iterator v5 = ((Map) v11).values().iterator();
            while (true) {
                label_70:
                if (!v5.hasNext()) {
                    return;
                }

                v2_1 = v5.next();
                if (v2_1 == null) {
                    continue;
                }

                if (((Article) v2_1).groupId <= 0) {
                    continue;
                }

                try {
                    synchronized (v9) {
                        Object v3_3 = ((Map) v9).get(((Article) v2_1).getItemKey());
                        if (v3_3 != null) {
                            v6 = ((Long) v3_3).longValue();
                        } else {
                            v6 = -1;
                        }
                        if (arg18 != v6) {
                            ((Map) v9).put(((Article) v2_1).getItemKey(), Long.valueOf(arg18));
                            v3_4 = 0;
                        } else {
                            v3_4 = 1;
                        }
                    }
                    if (v3_4 != 0) {
                        continue;
                    }
                } catch (Throwable v2_3) {
                    break;
                }

                if (v2_1 == null) {
                    continue;
                }

                DBHelper v4_2 = DBHelper.getInstance(this.c);
                if (arg21) {
                    if (v4_2 != null) {
                        v4_2.deleteArticle(((AbsArticle) v2_1));
                    }

                    Message v3_5 = this.g.obtainMessage(22);
                    v3_5.obj = v2_1;
                    this.g.sendMessage(v3_5);
                    continue;
                }

                v3_4 = 0;
                if (v4_2 != null) {
                    v3_4 = v4_2.a(((AbsArticle) v2_1)) != null ? 1 : 0;
                    v4_2.deleteArticleContent(((AbsArticle) v2_1));
                }

                Message message = this.g.obtainMessage(22);
                message.obj = v2_1;
                message.arg1 = 1;
                message.arg2 = v3_4 != 0 ? 1 : 0;
                this.g.sendMessage(message);
                return;
            }
        }
    }

    public final void a(long arg12, String arg14, JSONObject arg15) {
        int v0_4;
        long v2;
        long v8;
        int v4;
        LinkedHashMap v5;
        try {
            if (arg14.equals("rg")) {
                this.a(arg12, arg15, true);
                return;
            }

            if (arg14.equals("ug")) {
                this.a(arg12, arg15, false);
                return;
            }

            if (!arg14.equals("ra")) {
                return;
            }

            v5 = this.f;
            if (arg12 < 0) {
                arg12 = 0;
            }

            String v0_1 = arg15.optString("as");
            if (v0_1 == null) {
                return;
            }

            String[] v6 = v0_1.split(",");
            int v7 = v6.length;
            v4 = 0;
            while (true) {
                label_29:
                if (v4 >= v7) {
                    return;
                }

                v8 = Long.valueOf(v6[v4]).longValue();
                if (v8 > 0) {
                    synchronized (v5) {
                        Object v0_3 = ((Map) v5).get(Long.valueOf(v8));
                        if (v0_3 != null) {
                            v2 = ((Long) v0_3).longValue();
                        } else {
                            v2 = -1;
                        }

                        if (arg12 != v2) {
                            ((Map) v5).put(Long.valueOf(v8), Long.valueOf(arg12));
                            v0_4 = 0;
                        } else {
                            v0_4 = 1;
                        }
                    }
                    if (v0_4 != 0) {
                        ++v4;
                        continue;
                    }

                    if (v8 > 0) {
                        try {
                            Message v0_5 = this.g.obtainMessage(24);
                            v0_5.obj = Long.valueOf(v8);
                            this.g.sendMessage(v0_5);
                        } catch (Exception v0) {
                            return;
                        }
                    }

                    label_57:
                    ++v4;
                    continue;
                }
                ++v4;
                continue;
            }
        } catch (Exception v0) {
            return;
        }
    }

    public void handleMsg(Message message) {
        int v3;
        int v4;
        Object v0_1;
        int v1 = 1;
        switch (message.what) {
            case 21: {
                Object v1_1 = (message.obj instanceof Article) ? message.obj : null;
                if (v1_1 == null) {
                    return;
                }

                if (((Article) v1_1).groupId <= 0) {
                    return;
                }

                Article v0 = AppData.getInstance().e(((Article) v1_1).getItemKey());
                if (v0 != null) {
                    v0.a(((Article) v1_1));
                    Article v1_2 = v0;
                }

                Iterator v2 = this.a.iterator();
                while (true) {
                    if (!v2.hasNext()) {
                        return;
                    }

                    v0_1 = v2.next();
                    if (v0_1 == null) {
                        continue;
                    }
                    ((CmdHandler.a$a) v0_1).onGroupUpdate(((AbsArticle) v1_1));
                }
            }
            case 22: {
                if ((message.obj instanceof AbsArticle)) {
                    try {
                        v0_1 = message.obj;
                        if (message.arg1 == 0) {
                            v4 = 1;
                        } else {
                            v4 = 0;
                        }
                        if (message.arg2 == 1) {
                            v3 = 1;
                        } else {
                            v3 = 0;
                        }
                        if (v0_1 == null) {
                            return;
                        }

                        AppData v5 = AppData.getInstance();
                        if (v4 == 0) {
//                    goto label_75;
                        } else {
                            UpdateItemMgr.getInstance(this.c).e(((Article) v0_1).groupId);
                            String v1_3 = ((Article) v0_1).getItemKey();
                            if (v5.bt != null && v1_3 != null) {
                                v5.bt.b(v1_3);
                            }

                            Iterator v2 = this.a.iterator();
                            while (true) {
                                if (!v2.hasNext()) {
                                    return;
                                }
                                a$a v1_1 = (a$a) v2.next();
                                if (v1_1 == null) {
                                    continue;
                                }
                                v1_1.onGroupDeleted(((AbsArticle) v0_1));
                            }
                        }


                        label_75:
                        if (v3 == 0) {
                            if (v5.e(((Article) v0_1).getItemKey()) == null) {
                                v1 = 0;
                            }
                        } else {
                            v1 = v3;
                        }

                    } catch (Exception v0_2) {
                        label_69:
                        Logger.e("CommandHandler", "exception in handlemsg : " + v0_2.toString());
                        return;
                    }

                    label_79:
                    if (v1 == 0) {
                        return;
                    }

                    try {
                        new RefreshGroupThread(this.c, this.g, ((Article) v0_1)).start();
                    } catch (Exception v0_2) {
                    }
                }
            }
            case 24: {
                if (!(message.obj instanceof Long)) {
                    return;
                }

                try {
                    this.a(((Long) message.obj).longValue());
                } catch (Exception v0_2) {
                    Logger.e("CommandHandler", "exception in handlemsg : " + v0_2.toString());
                }

                return;
            }
        }
        return;
    }

    public interface a$a {
        void onAdDeleted(long arg1);

        void onGroupDeleted(AbsArticle arg1);

        void onGroupUpdate(AbsArticle arg1);
    }

    final static class RefreshGroupThread extends AbsApiThread {
        private Context mContext;
        private Handler mHandler;
        private Article mArticle;

        public RefreshGroupThread(Context context, Handler handler, Article article) {
            super("RefreshGroupThread", Priority.LOW);
            this.mContext = context;
            this.mHandler = handler;
            this.mArticle = article;
        }

        private static Article a(DBHelper dbHelper, Article article) {
            ArticleDetail articleDetail = null;
            try {
                articleDetail = ArticleQueryThread.getArticleDetail(dbHelper, article, true, "");
            } catch (Throwable throwable) {
                Logger.e("CommandHandler", "exception in AppData : " + throwable.toString());
            }

            if (articleDetail != null) {
                return articleDetail.article;
            }
            return null;
        }

        public final void run() {
            if (Logger.debug()) {
                Logger.d("CommandHandler", "RefreshGroupThread : start");
            }

            DBHelper v0 = DBHelper.getInstance(this.mContext);
            if (v0 != null) {
                Article v1 = RefreshGroupThread.a(v0, this.mArticle);
                if (v1 != null) {
                    v0.a(v1);
                    this.mHandler.sendMessage(this.mHandler.obtainMessage(21, v1));
                }

                if (!Logger.debug()) {
                    return;
                }

                Logger.d("CommandHandler", "RefreshGroupThread : stop");
            }
        }
    }
}

